//
//  MYXPhotoauthenticationViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/7.
//  Copyright © 2017年 hbjApple. All rights reserved.
//实名认证图片

#import "MYXPhotoauthenticationViewController.h"
#import "MYXPhotoauthenticationCell.h"
#import "MYXUpdataconfigurator.h"
#import "MYXAuthenticationModel.h"
#import "JXTActionSheet.h"
#import "MYXAuthenticationModel.h"


static NSString *const MYXPhotoauthenticationCellIdentifier = @"MYXPhotoauthenticationCell";
@interface MYXPhotoauthenticationViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,UIAlertViewDelegate,JXTActionSheetDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong)MYXUpdataconfigurator *configurator;

@end

@implementation MYXPhotoauthenticationViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
#pragma mark - Data
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

//- (MYXAuthenticationModel *)authenticationMod{
//    if ( self.authenticationMod == nil) {
//        self.authenticationMod = [[MYXAuthenticationModel alloc] init];
//    }
//    return self.authenticationMod;
//}
- (void)setAuthenticationMod:(MYXAuthenticationModel *)authenticationMod{
    _authenticationMod = authenticationMod;
    [self.tableView reloadData];
}



-(void)leftButt
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 44, 44);
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setLeftBarButtonItem:cancelItem animated:NO];
    
}
-(void)exit
{
    //为了防止键盘的闪屏，延迟0.25秒退出
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.PhotoauthenBlock) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
            self.PhotoauthenBlock(self.authenticationMod);
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    });
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self leftButt];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:MYXPhotoauthenticationCellIdentifier bundle:nil] forCellReuseIdentifier:MYXPhotoauthenticationCellIdentifier];
}


- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
    }
    return _tableView;
}
#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYXPhotoauthenticationCell *MYXPhotoauthenticationC = (MYXPhotoauthenticationCell *)[tableView dequeueReusableCellWithIdentifier:MYXPhotoauthenticationCellIdentifier];
    MYXPhotoauthenticationC.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row==0) {
        if (self.authenticationMod.image_front) {
            [MYXPhotoauthenticationC.photoImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MYXIMGBaseUrl,self.authenticationMod.image_front]] placeholderImage:[UIImage imageNamed:@"shengfenzhengzhengmian"]];
            
        }else{
            MYXPhotoauthenticationC.photoImageView.image =[UIImage imageNamed:@"shengfenzhengzhengmian"];
        }
        
    }else{//self.MYXAuthenticationMod.image_back
        if (self.authenticationMod.image_back) {
            [MYXPhotoauthenticationC.photoImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MYXIMGBaseUrl,self.authenticationMod.image_back]] placeholderImage:[UIImage imageNamed:@"shengfenzhengfanmian"]];
            
        }else{
            MYXPhotoauthenticationC.photoImageView.image =[UIImage imageNamed:@"shengfenzhengfanmian"];
        }
    }

    return MYXPhotoauthenticationC;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 194;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self configura:indexPath.row];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView =[[UIView alloc]init];
    headView.backgroundColor =[UIColor whiteColor];
    headView.frame = CGRectMake(0, 0, WIDTH, 45);
    UILabel * _titileLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, headView.frame.size.width-30,headView.frame.size.height)];
    _titileLabel.font = MYYFont(15.0f);
    _titileLabel.textColor =MYYtitleColor ;
    _titileLabel.text = @"请上传照片";
    [headView addSubview:_titileLabel];
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *FooterView =[[UIView alloc]init];
    FooterView.frame = CGRectMake(0, 0, WIDTH, 0.1);
    return FooterView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(void)configura:(NSInteger )tag{
    _configurator = [[MYXUpdataconfigurator alloc] init];
    [_configurator setup:self type:2];
    WS(weakSelf)
    _configurator.SucessBlock  =^(NSString * imageData,UIImage * image){
        if (tag==0) {
            weakSelf.authenticationMod.image_front=imageData;
        }else if (tag==1){
             weakSelf.authenticationMod.image_back=imageData;
        }
        [weakSelf.tableView reloadData];
    };
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
